package com.company;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Auther zxh
 * @Date 2023/3/64:49 PM
 */
public class WordCount {
    static ArrayList<Integer> chartCountList = new ArrayList<>();//中文字数

    public static void main(String[] args) throws Exception {
        File f = new File("/Users/dasouche-data/Documents/2301版本");
        //文件格式
        String type = ".txt";
        treeFile(f,type);
        System.out.println("中文字数："+chartCountList.stream().mapToInt(Integer::intValue).sum());

    }

    public static void treeFile(File f, String type) throws Exception {
        File[] listFiles = f.listFiles();
        for (int i = 0; i < listFiles.length; i++) {
            File file = listFiles[i];
            if (!file.isDirectory()) {
                if (file.getName().endsWith(type)) {
                    BufferedReader br = null;
                    br = new BufferedReader(new FileReader(file));
                    String line = "";
                    while ((line = br.readLine()) != null) {
                        getChart(line,chartCountList);
                    }
                    if (br != null) {
                        br.close();
                    }
                }
            } else {
                treeFile(listFiles[i], type);
            }

        }

    }

    public static void getChart(String line,List chartCount){
        String regEx = "[^\u4e00-\u9fa5]+";
        String[] split = line.split(regEx);
        for (int i = 0; i < split.length; i++) {
            chartCount.add(split[i].length());
        }
    }
}
